package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EClick;
	import classes.controller.EDataObject;
	import classes.controller.ENumber;
	import classes.controller.ESendJS;
	import classes.model.VOIsLocal;
	
	import com.d4p.custom.defaultButton.DefaultBtn;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class ListRoom extends Sprite
	{
		private var _paging:int = 1;
		private var _totalPage:int;
		
		private var _sortContainer:SortContainer;
		private var _createroomContainer:CreateroomContainer;
		private var _subjectBtn:DefaultBtn;
		private var _pagingContainer:PagingContainer;
		private var _listContainer:ListContainer;
		
		private var _nextBtn:DefaultBtn;
		private var _prevBtn:DefaultBtn;
		
		private var _listroom_switch:Boolean;
		
		public function ListRoom()
		{
			super();
			setLayOut();
			defaultSetting();
			dataListener();
		}
		
		public function set listroom_switch($switch:Boolean):void
		{
			if($switch){
				initListener();
				this.visible = true;
				
				if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
					//test.
					var loader:URLLoader = new URLLoader();
					loader.addEventListener( Event.COMPLETE,	onDataLoaded);
					try {
						loader.load( new URLRequest('sample/list.json'));
					}catch ( error:Error ) {
						trace( "Unable to load requested document." );
					}
				}
			}else{
				this._createroomContainer.resetRoom();
				removeListener();
				this.visible = false;
				this._listContainer.resetList();
			}
		}
		
		private function dataListener():void
		{
			DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_ROOMLIST,	onloadcomplete);	
		}
		
		private function onDataLoaded(e:Event):void
		{
			var jsonParsed:*  = JSON.parse(e.target.data);
			var obj:Object 	= jsonParsed.data;
			
			dataSetting = obj[0];
			dispatchEvent(new Event("loadingOff"));
		}
		
		private function onloadcomplete(e:ESendJS):void
		{
			var obj:Object 	= e._obj;
			dataSetting = obj;
			dispatchEvent(new Event("loadingOff"));
		}
		
		public function set dataSetting($obj:Object):void
		{
			_paging 		= $obj.nowpage;
			_totalPage 		= $obj.totalpage; 
			
			this._pagingContainer.pageOn(_paging, _totalPage); // paging.
			this._listContainer.listOn = $obj.page;
			
			_listContainer.page 	= _paging;
			_listContainer.sortNum 	= _typeNum;
			
			this.nextPrevEnabled(_paging, _totalPage);
		}
		
		
		private function setLayOut():void
		{
			this._sortContainer 		= this.getChildByName('sortcontainer_mc')	as SortContainer;
			this._createroomContainer	= this.getChildByName('createroom_mc')		as CreateroomContainer;
			this._subjectBtn			= this.getChildByName('subject_btn')		as DefaultBtn;
			this._pagingContainer		= this.getChildByName('paging_mc')			as PagingContainer;
			this._listContainer			= this.getChildByName('list_mc')			as ListContainer;
			this._nextBtn				= this.getChildByName('next_btn')			as DefaultBtn;
			this._prevBtn				= this.getChildByName('prev_btn')			as DefaultBtn;
		}
		
		private function defaultSetting():void
		{
			this.visible = false;
		}
		
		private function initListener():void
		{
			this._sortContainer.addEventListener(EClick.EVENT_CLICK,			onClickSortHandler);
			this._createroomContainer.addEventListener(EClick.EVENT_CLICK,		onClickHandler);
			this._nextBtn.addEventListener(MouseEvent.CLICK,					onClickPagingHandler);
			this._prevBtn.addEventListener(MouseEvent.CLICK,					onClickPagingHandler);
			this._pagingContainer.addEventListener(ENumber.EVENT_NUMBER,		onClickPagingContainer);
			this._subjectBtn.addEventListener(MouseEvent.CLICK,					onClickSubjectHandler);
			this._listContainer.addEventListener(EDataObject.EVENT_DATAOBJECT,	dispatchJoinRoom);
		}
		
		private function removeListener():void
		{
			this._sortContainer.removeEventListener(EClick.EVENT_CLICK,				onClickSortHandler);
			this._createroomContainer.removeEventListener(EClick.EVENT_CLICK,		onClickHandler);
			this._nextBtn.removeEventListener(MouseEvent.CLICK,						onClickPagingHandler);
			this._prevBtn.removeEventListener(MouseEvent.CLICK,						onClickPagingHandler);
			this._pagingContainer.removeEventListener(ENumber.EVENT_NUMBER,			onClickPagingContainer);
			this._subjectBtn.removeEventListener(MouseEvent.CLICK,					onClickSubjectHandler);
			this._listContainer.removeEventListener(EDataObject.EVENT_DATAOBJECT,	dispatchJoinRoom);
		}
		
		//room으로 들어가기위한 이벤트 발생. 
		private function dispatchJoinRoom(e:EDataObject):void
		{
			//listroom off.
			this.listroom_switch	= false;
			dispatchEvent(new EDataObject(e._object,	EDataObject.EVENT_DATAOBJECT));
		}
		
		private function onClickSubjectHandler(e:MouseEvent):void
		{
			dispatchEvent(new EClick('subject', EClick.EVENT_CLICK));
		}
		
		
		//수정
		private function onClickHandler(e:EClick):void
		{
			
			if(e._click=='go'){
				this.listroom_switch = false;	
			}
			dispatchEvent(new EClick(e._click, EClick.EVENT_CLICK));
		}
		
		private var _typeNum:int=0;
		private function onClickSortHandler(e:EClick):void
		{
			if(e._click == "emptyRoom"){
				_typeNum = 1;
			}else{
				_typeNum = 0;
			}
			
			var obj:Object;
			obj = {
				"data":[
					{
						"page":1
						,"limit":6
						,"type":_typeNum
					}
				]
			};
			
			_listContainer.sortNum = _typeNum;
			_listContainer.page = 1;
			
			DAOSendFlash.getLoadInstance().setData("getRoomList", obj);	
		}
		
		private function onClickPagingHandler(e:MouseEvent):void
		{
			var obj:Object;
			switch(e.currentTarget){
				case _nextBtn :
					//trace('click : "next"');
					dispatch_paging = _paging+1;

					break;
				case _prevBtn :
					//trace('click : "prev"');
					dispatch_paging = _paging-1;
					break;
			}
		}
		
		private function nextPrevEnabled($pageNum:int, $totalNum:int):void
		{
			if($pageNum == $totalNum){
				_nextBtn.alpha = 0.3;
				_nextBtn.mouseEnabled  = false;
				_nextBtn.mouseChildren = false;
			}else{
				_nextBtn.alpha = 1;
				_nextBtn.mouseEnabled  = true;
				_nextBtn.mouseChildren = true;
			}
			
			if($pageNum == 1){
				_prevBtn.alpha = 0.3;
				_prevBtn.mouseEnabled  = false;
				_prevBtn.mouseChildren = false;
			}else{
				_prevBtn.alpha = 1;
				_prevBtn.mouseEnabled  = true;
				_prevBtn.mouseChildren = true;
			}
		}
		
		private function onClickPagingContainer(e:ENumber):void
		{
			
			dispatch_paging = e._num;
		}
		
		//paging.
		private function set dispatch_paging($num:int):void
		{
			var obj:Object;
			obj = {
				"data":[
					{
						"page":$num
						,"limit":6
						,"type":_typeNum
					}
				]
			};
			
			_listContainer.page 	= $num;
			_listContainer.sortNum 	= _typeNum;
			DAOSendFlash.getLoadInstance().setData("initPageNum", obj);	
			//dispatchEvent(new EDataObject("loading",	EDataObject.EVENT_DATAOBJECT));
		}
	}
}